package com.gugu.procedure.util;

import javax.crypto.Cipher;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.spec.EncodedKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class RSAUtil {
    public static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCr2PU6e7U57Sa6GmE7mMCmPn2HeRLEWsZBBmFeDXF1GiUFbnkuGoYHgfOMB0xoSfMJt+JgZYs5bEvt+T2EZqXfazAXy23gOtn80KgLTOIkX4/reBJBbRQvYwCEeajYgcAzY9zJoH8CO3LA4hXr6gYCDF5oiTGf4NlWKcxPQIzSAQIDAQAB";
    public static String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKvY9Tp7tTntJroa" +
            "YTuYwKY+fYd5EsRaxkEGYV4NcXUaJQVueS4ahgeB84wHTGhJ8wm34mBlizlsS+35" +
            "PYRmpd9rMBfLbeA62fzQqAtM4iRfj+t4EkFtFC9jAIR5qNiBwDNj3MmgfwI7csDi" +
            "FevqBgIMXmiJMZ/g2VYpzE9AjNIBAgMBAAECgYEAjgtQcF3u54mh+DRz6IoTu5Zt" +
            "tjwaihkqRHdcphxgitXogfvVyIj9/lipEja6Ln3pmT/Yq6tjkDx26qaY9cbPFWmq" +
            "m739lNRsGB8NeAdZuW/irPKUABJV/VnXJyX0Yexk1+QI0khip481K7xEHYGOpDCC" +
            "akg106ZHYBji3gG4HZECQQDXd1myiEv4fdbeySJU/PIfegv+6kHSsJAMmqNf4w76" +
            "ridlnD2IfrW7EMmRXw9Q3yhhOAA2n3RkaMZBeXarzvC1AkEAzCz4mXqZDk0ZSdo5" +
            "9aRrzEZphEa3t7xlyHdjawx4ogIuWth6vCkmEUHg2gNSNJiTx0GO3iQ65qzN9xUX" +
            "D/1HnQJAPtZ+4VNgnJrOlHCiQ5j1X/qvDuvwwGpfyF088lT85Fzv/nE/fNQ/Wcv+" +
            "jRflFpfOZfP4E/YS8smfsC6lTlvu1QJAYzXy8nj9CFL0V8t/YFotu0v3l7vZJ6GW" +
            "/6rrlyevMmHG7oPEV8vS4I3XWXhFqKL/I9I6PrzyFgaf6lDILQ/QjQJBALRYT6dl" +
            "UXUqbUvC4S3eZrC5eEec+QQUjheSNZ333co8FJdrRNH0Y+UFbDPhzWPkpueBEh62" +
            "+KCP/fqTuAM0DN4=";

    public static void main(String[] args) throws GeneralSecurityException {

        String encryptedString = encrypt("7712345678909@_@NC-YD-01,7712345678908@_@NC-YD-01", publicKey);
        System.out.println(encryptedString);
        String decryptedString = RSAUtil.decrypt(encryptedString, privateKey);
        System.out.println(decryptedString);

    }

    // 加密
    public static String encrypt(String data, String publicKey) throws GeneralSecurityException {
        EncodedKeySpec keySpec = new X509EncodedKeySpec(base64Decode(publicKey));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePublic(keySpec));
        return base64Encode(cipher.doFinal(data.getBytes()));
    }

    //解密
    public static String decrypt(String data, String privateKey) throws GeneralSecurityException {
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(base64Decode(privateKey));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, keyFactory.generatePrivate(keySpec));
        return new String(cipher.doFinal(base64Decode(data)));
    }

    static String base64Encode(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }

    static byte[] base64Decode(String str) {
        return Base64.getDecoder().decode(str);
    }
}
